package com.example.lx5_22

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.example.base.entity.ParkEntityItem
import com.example.base.mvvm.BaseActivity
import com.example.lx5_22.databinding.ActivityAddPackBinding
import com.example.lx5_22.park.ParkViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/app/AddParkActivity")
class AddPackActivity : BaseActivity<ActivityAddPackBinding, ParkViewModel>() {

    var typea = "标准车位"
    var area = "A区停车场"
    var areaId = 1
    var status = "空闲"

    override fun initView() {
        binding.ivBack.setOnClickListener {
            finish()
        }

        binding.rgParkingStatus.setOnCheckedChangeListener { radioGroup, i ->
            when(i){
                R.id.rbFree ->
                    status = "空闲"
                R.id.rbOccupied ->
                    status = "已占用"
                R.id.rbMaintenance ->
                    status = "维护中"
            }
        }

        binding.ivYes.setOnClickListener {
            val carNo = binding.etParkingNumber.text.toString()
            val remark = binding.etRemark.text.toString()
            val item = ParkEntityItem(areaId,area,null,"",remark,carNo,status,typea,1,"梅子雄")
            viewModel.getAddPark(item)
            finish()
        }

        binding.etRemark.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(s: CharSequence?, p1: Int, p2: Int, p3: Int) {
                val length = s?.length
                binding.tvCharCount.text = "${length}/10"
                if(length == 10){
                    binding.tvCharCount.setTextColor(Color.RED)
                }else{
                    binding.tvCharCount.setTextColor(Color.GRAY)
                }
            }

            override fun afterTextChanged(s: Editable?) {

            }

        })

        val areas = arrayOf("A区停车场","B区停车场","C区停车场")
        val areaAdapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, areas)
        areaAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.etParkingArea.adapter = areaAdapter
        binding.etParkingArea.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(
                p0: AdapterView<*>?,
                p1: View?,
                p2: Int,
                p3: Long
            ) {
                area = areas[p2]
            }

            override fun onNothingSelected(p0: AdapterView<*>?) {

            }

        }

        val types = arrayOf("标准车位","VIP车位")
        val typeAdapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, types)
        typeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.etParkingType.adapter = typeAdapter
        binding.etParkingType.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(
                p0: AdapterView<*>?,
                p1: View?,
                p2: Int,
                p3: Long
            ) {
                typea = types[p2]
            }

            override fun onNothingSelected(p0: AdapterView<*>?) {

            }

        }

    }

    override fun initData() {
        viewModel.addParkData.observe(this){
            ToastUtils.showLong("添加成功")
        }
    }

}